//
//  CZVistorView.swift
//  CZWeibo
//
//  Created by 左凯旭 on 16/8/9.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
import SnapKit
class CZVistorView: UIView {

    
   override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //声明一个闭包
    var closure:(()->())?
    
    
    
     // MARK: - 供外界调用的方法
    func setupVisitorInfo(imageName:String?,title:String?){
         // 代表设置的是消息发现我页面
        if let img = imageName,let tit = title {
            
            iconImageView.image = UIImage(named: img)
            messageLabel.text = tit
            
            circleImageView.hidden = true
            
        }else{
            //代表为首页
            setupAnim()
        }
        
        
    }
    
    //MARK: - 设置圆圈动画
    func setupAnim(){
        
        let animation = CABasicAnimation(keyPath: "transform.rotation")
            // 设置toValue
        animation.toValue = M_PI * 2
        
        //设置动画时长
        animation.duration = 10
        
        //不让动画移除
        animation.removedOnCompletion = false
        
        //设置重复次数
        animation.repeatCount = MAXFLOAT
        
        //添加动画
        circleImageView.layer.addAnimation(animation, forKey: nil)
        
    }
    
    
    // MARK: - 注册按钮的监听
   @objc private func buttonClick(){

        //执行闭包
        closure?()
    }
    
    
    
    private func setupUI(){
        
       backgroundColor = RGB(237, green: 237, blue: 237)
        //添加控件
        addSubview(circleImageView)
        addSubview(maskImageView)
        addSubview(iconImageView)
        addSubview(messageLabel)
        addSubview(registerBtn)
        addSubview(loginBtn)
        
        //设置约束
        circleImageView.snp_makeConstraints { (make) in
            
            make.center.equalTo(self)
        }
        
        maskImageView.snp_makeConstraints { (make) in
            
            make.center.equalTo(self)
        }
        
        iconImageView.snp_makeConstraints { (make) in
            make.center.equalTo(self)
        }
        
        messageLabel.snp_makeConstraints { (make) in
            
            make.top.equalTo(maskImageView.snp_bottom).offset(16)
            make.centerX.equalTo(self)
            make.height.equalTo(36)
        }
        
        registerBtn.snp_makeConstraints { (make) in
            make.top.equalTo(messageLabel.snp_bottom).offset(16)
            make.leading.equalTo(messageLabel.snp_leading)
            make.size.equalTo(CGSize(width: 100,height: 36))
        }
        
        loginBtn.snp_makeConstraints { (make) in
            make.size.top.equalTo(registerBtn)
            make.trailing.equalTo(messageLabel.snp_trailing)
        }
        
    }
    
     // MARK: - 懒加载控件
    private lazy var circleImageView: UIImageView = UIImageView(imageName: "visitordiscover_feed_image_smallicon")
    
    private lazy var maskImageView: UIImageView = UIImageView(imageName: "visitordiscover_feed_mask_smallicon")
    
    private lazy var iconImageView: UIImageView = UIImageView(imageName: "visitordiscover_feed_image_house")
    
    private lazy var messageLabel:UILabel = {
        let label = UILabel()
        label.textColor = AppThemeColor
        label.textAlignment = .Center
        label.font = UIFont.systemFontOfSize(15)
        label.numberOfLines = 0
        label.preferredMaxLayoutWidth = 240
        label.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        return label
    }()
    
    private lazy var registerBtn:UIButton = {
       
        let button = UIButton()
        button.addTarget(self, action: "buttonClick", forControlEvents: UIControlEvents.TouchUpInside)
        button.setTitle("注册", forState: UIControlState.Normal)
        button.setBackgroundImage((UIImage(named: "common_button_white_disable")), forState: UIControlState.Normal)
        button.setTitleColor(AppThemeColor, forState: UIControlState.Normal)
        
        return button
    }()
    
    private lazy var loginBtn:UIButton = {
       let button = UIButton()
        button.addTarget(self, action: "buttonClick", forControlEvents: UIControlEvents.TouchUpInside)
        button.setTitleColor(AppThemeColor, forState: UIControlState.Normal)
        button.setBackgroundImage((UIImage(named: "common_button_white_disable")), forState: UIControlState.Normal)
        button.setTitle("登陆", forState: UIControlState.Normal)
        
        return button
    }()
    
}










